<?php

namespace App\Admin\Forms;


use App\Models\SwtEmployee;
use App\Models\SwtProcurement;
use App\Models\SwtProcurementGoods;
use App\Models\SwtSupplier;
use Dcat\Admin\Form\NestedForm;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
use Dcat\Admin\Contracts\LazyRenderable;
use Illuminate\Support\Facades\DB;


class StartProcurementForm extends Form implements LazyRenderable
{
    use LazyWidget;
    public function handle(array $input)
    {
        $id = $this->payload['id'];

        Db::beginTransaction();
        $num = 0;
        $data = ['procurement_type'=>2];
        try {
            if($input['supplice_type'] == 1){
                $supplier = SwtSupplier::where('id',$input['supplice_id'])->first();
                $data['supplice_id'] = $input['supplice_id'];
                $data['supplice_name'] = $supplier->supplier_name;
                $data['supplice_type'] = $input['supplice_type'];
            }else{
                $employee = SwtEmployee::where('id',$input['operator_id'])->first();
                $data['operator_id'] = $input['operator_id'];
                $data['operator_name'] = $employee->name;
                $data['supplice_type'] = $input['supplice_type'];
            }
            if(!SwtProcurement::where('id',$id)->update($data)){
                $num ++;
            }

            if($num > 0) {
                throw new \Exception('开始采购异常，请重试');
            }

            Db::commit();
            return $this->response()->success('开始采购')->refresh();
        } catch (\Exception $e) {
            Db::rollBack();
            return $this->response()->error('开始失败');
        }
    }


    public function form()
    {
        $this->text('procurement_no', '采购单号')->disable();
        $this->display('procurement_type', '状态')->disable();
        $this->select('supplice_type', '采购方式')
            ->options([1=>'供应商',2=>'自采'])
            ->when(1, function (Form $form) {
                // 为1 时 拿供应商数据
                $form->select('supplice_id','供应商')
                    ->options(function () {
                        $supplier_new = [];
                        $supplier = SwtSupplier::select('id','supplier_name')->get()->toArray();
                        foreach ($supplier as $value) {
                            $supplier_new[$value['id']] = $value['supplier_name'];
                        }
                    return $supplier_new;
                });
            })->when(2,function (Form $form) {
                //为2 时拿采购员数据
                $form->select('operator_id','采购员')->options(function () {
                    $supplier_new = [];
                    $supplier = SwtEmployee::select('id','name')->where('type',1)->get()->toArray();
                    foreach ($supplier as $value) {
                        $supplier_new[$value['id']] = $value['name'];
                    }
                    return $supplier_new;
                });
            });



        $this -> table('goods','商品明细', function(NestedForm $table) {
            $table -> hidden('id');
            $table -> display('goods_name', '商品名');
            $table -> display('advance_price', '采购价格');

            $table -> text('advance_quantity','采购数量');


        })->disableCreate()->disableDelete()->default($data['goods'] ?? []);

    }

    public function default()
    {
        $id = $this->payload['id'];

        $procurement_info = SwtProcurement::where('id', $id)->first();
        $goods_info  = SwtProcurementGoods::with(['goods']) -> where('procurement_id', $id)->get()->toArray();
        $status_array = ['1'=>'预采购','2'=>'采购中','3'=>'采购完成','0'=>'关闭'];
        $goods = [];

        foreach($goods_info as $key=>$value){
            $goods[$key] = [
                'id' => $value['good_id'],
                'goods_name' => $value['goods']['goods_name'],
                'advance_price' => bcdiv($value['advance_price'],100,2),
                'advance_quantity' => $value['advance_quantity'],
            ];
        }

        $data = [
            'procurement_no' => $procurement_info->procurement_no,
            'procurement_type' => $status_array[$procurement_info->procurement_type],
            'goods' => $goods,
        ];
        return $data;
    }


}
